/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "@/plugins/ai-warning/alarm/api/rdAlarmMsg.ts";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const msg = useMessage();

  // 获取API基础URL
  const env = import.meta.env;
  const baseUrl =
    env.VITE_OPEN_PROXY === "true"
      ? env.VITE_PROXY_PREFIX
      : env.VITE_APP_API_BASEURL;

  function filterImgUrl(url: string) {
    if (!url) {
      return defaultAvatar;
    }
    if (url.startsWith("http")) {
      return url;
    }
    return `${baseUrl}${url}`;
  }

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    {
      label: "报警事件名称",
      prop: "result_data.name",
      showOverflowTooltip: true,
    },
    {
      label: "报警图片",
      prop: "alarm_pic_path",
      cellRender: ({ row }) => (
        <el-image
          src={filterImgUrl(row.alarm_pic_path)}
          style={{ width: "80px", height: "60px", borderRadius: "4px" }}
          fit="cover"
          preview-src-list={[filterImgUrl(row.alarm_pic_path)]}
        />
      ),
    },
    {
      label: "摄像头地址",
      prop: "camera_url",
      showOverflowTooltip: true,
    },
    {
      label: "图片大小",
      prop: "image_size",
      cellRender: ({ row }) => {
        if (row.image_width && row.image_height) {
          return `${row.image_width} × ${row.image_height}`;
        }
        return "-";
      },
    },
    {
      type: "operation",
      label: () => t("crud.operation"),
      fixed: "right",
      width: 100,
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => hasAuth("alarm:rdAlarmMsg:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) => hasAuth("alarm:rdAlarmMsg:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
